package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.LeaveTeacherStatus;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IScClaTimeService;
import com.ruoyi.web.core.config.GeTuiUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISysMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息推送Controller
 * 
 * @author ruoyi
 * @date 2024-02-11
 */
@RestController
@RequestMapping("/system/message")
@Transactional(rollbackFor = {Exception.class})
public class SysMessageController extends BaseController
{
    @Autowired
    private ISysMessageService sysMessageService;

    @Autowired
    private SysMessageMapper sysMessageMapper;

    @Autowired
    private IScClaTimeService scClaTimeService;




    @Autowired
    private SysTeacherUserMapper sysTeacherUserMapper;

    @Autowired
    private SysUserStudentMapper sysUserStudentMapper;

    @Autowired
    private GeTuiUtils geTuiUtils;
    @Autowired
    private SysMessageInfoMapper sysMessageInfoMapper;
    @Autowired
    private SysUserCidMapper sysUserCidMapper;



    /**
     * 查询消息推送列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMessage sysMessage)
    {
        startPage();
        List<SysMessage> list = sysMessageService.selectSysMessageList(sysMessage);
        return getDataTable(list);
    }



    /**
     * 查询消息推送列表
     */
//    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @PostMapping("/teacherMessageList")
    public AjaxResult teacherMessageList(HttpServletRequest request)
    {
        Long RecId = Long.parseLong(request.getParameter("recid"));
        List<SysMessage> list = sysMessageService.selectRecidSysMessageList(RecId);
        return AjaxResult.success(list);
    }
    /**
     * 查询个人所有请假消息
     */
    @GetMapping("/StudentLeaveMessageList")
    public AjaxResult StudentLeaveMessageList(HttpServletRequest request)
    {
        Long sendId = Long.parseLong(request.getParameter("sendId"));
        List<SysMessage> list = sysMessageService.selectSendIdSysMessageList(sendId);
        return AjaxResult.success(list);
    }
    /**
     *  查询某一条请假信息
     */
    @GetMapping("/message_info_id")
    public AjaxResult message_info_id(HttpServletRequest request)
    {
        Long message_info_id = Long.parseLong(request.getParameter("message_info_id"));
        List<LeaveTeacherStatus> list = sysMessageService.selectMessage_info_idSysMessageList(message_info_id);
        return AjaxResult.success(list);
    }

    /**
     * 导出消息推送列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:export')")
    @Log(title = "消息推送", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMessage sysMessage)
    {
        List<SysMessage> list = sysMessageService.selectSysMessageList(sysMessage);
        ExcelUtil<SysMessage> util = new ExcelUtil<SysMessage>(SysMessage.class);
        util.exportExcel(response, list, "消息推送数据");
    }

    /**
     * 获取消息推送详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:message:query')")
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") Long messageId)
    {
        return AjaxResult.success(sysMessageService.selectSysMessageByMessageId(messageId));
    }

    /**
     * 新增消息推送
     */
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @Log(title = "消息推送", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMessage sysMessage)
    {
        return toAjax(sysMessageService.insertSysMessage(sysMessage));
    }

    /**
     * 新增请假消息推送
     */
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @PostMapping("/leave")
    public AjaxResult addleave(@RequestBody SysMessage sysMessage)
    {

        Long[] teacherIds = sysMessage.getTeacherIds();
        SysMessageInfo sysMessageInfo = new SysMessageInfo();
        sysMessageInfo.setStartTime(sysMessage.getStartTime());
        sysMessageInfo.setEndTime(sysMessage.getEndTime());
        sysMessageInfo.setInfoContent(sysMessage.getInfo_content());
        sysMessageInfoMapper.insertSysMessageInfo(sysMessageInfo);


        for (Long id: teacherIds
             ) {

            sysMessage.setMessage_info_id(sysMessageInfo.getMessageInfoId());
            SysTeacherUser sysTeacherUser = sysTeacherUserMapper.selectSysTeacherUserByTeacherId(id);
            sysMessage.setRecId(sysTeacherUser.getUserId());
            sysMessageService.insertSysMessage(sysMessage);
            SysUserCid sysUserCid = sysUserCidMapper.selectSysUserCidByUserId(sysTeacherUser.getUserId());
            geTuiUtils.pushToSingleByCid(sysUserCid.getCid(),"通知消息","你有一条新的消息~");



        }

        return AjaxResult.success();
    }
    /**
     * 审批消息推送
     */
//    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @Log(title = "消息推送", businessType = BusinessType.UPDATE)
    @PostMapping("/editLeave")
    public AjaxResult editLeave(@RequestBody SysMessage sysMessage)
    {
        sysMessageMapper.updateSysMessageByMessageId(sysMessage.getMessageId());
        List<SysMessage> sysMessages = sysMessageMapper.selectSysMessageBymessage_info_id(sysMessage.getMessage_info_id());
        //查看请假消息是否全部审批完毕
        for (SysMessage item:
                sysMessages) {
            if(item.getStatus() != 1){
                return AjaxResult.success();
            }

        }



        scClaTimeService.searchListForMessageLeave(sysMessage);

        String cid = sysUserStudentMapper.selectBystudentId(sysMessage.getSendId());
        geTuiUtils.pushToSingleByCid(cid,"通知消息","你有一条新的消息~");




        return  AjaxResult.success();
    }

    /**
     * 审批消息推送
     */
//    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @Log(title = "消息推送", businessType = BusinessType.UPDATE)
    @PostMapping("/editRefuseLeave")
    public AjaxResult editRefuseLeave(@RequestBody SysMessage message)
    {
        sysMessageMapper.updateRefuseSysMessageByMessageId(message);
        return  AjaxResult.success();
    }

    /**
     * 修改消息推送
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @Log(title = "消息推送", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMessage sysMessage)
    {
        return toAjax(sysMessageService.updateSysMessage(sysMessage));
    }

    /**
     * 删除消息推送
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @Log(title = "消息推送", businessType = BusinessType.DELETE)
	@DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds)
    {
        return toAjax(sysMessageService.deleteSysMessageByMessageIds(messageIds));
    }
}
